package com.example.zzbus.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.example.zzbus.R;
import com.example.zzbus.ui.map.MapFragment;
import com.example.zzbus.ui.me.MeFragment;
import com.example.zzbus.ui.rec.RecFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    MapFragment mapFragment;
    RecFragment recFragment;
    MeFragment meFragment;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    replaceFragmentToActivity(mapFragment);
                    return true;
                case R.id.navigation_dashboard:
                    replaceFragmentToActivity(recFragment);
                    return true;
                case R.id.navigation_notifications:
                    replaceFragmentToActivity(meFragment);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
         mapFragment=new MapFragment();
         recFragment=new RecFragment();
         meFragment=new MeFragment();
        replaceFragmentToActivity(mapFragment);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }
    /**
     * 替换fragment
     * @param
     * @param fragment
     */
    public  void replaceFragmentToActivity( @NonNull Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, fragment);
        transaction.commit();
    }


}
